home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / RectangleLib / RectangleLib.c next >
Encoding:
C/C++ Source or Header  |  1994-01-14  |  3.5 KB  |  153 lines  |  [TEXT/KAHL]

  1. /* Revision History:
  2.     
  3.     94/01/14 aih
  4.     - added to function to calculate a rectangle's area
  5.     
  6.     93/11/07 aih
  7.     - made some parameters const
  8.     
  9.     91/05/14 AIH
  10.     - Fixed mistake in function for moving rectangles
  11.     
  12.     91/05/12 AIH
  13.     - Rectangles are positioned on the current screen, not the main screen
  14.     
  15.     91/04/26 AIH
  16.     - Added function to move a rectangle
  17.     
  18.     91/03/20 AIH
  19.     - Sometime earlier this month I added functions to convert a rectangle
  20.     to global or local coordinates
  21.     - RectValid always validates the rectangle, not just when debugging,
  22.     since other code relies on RectValid to work all the time.
  23.     
  24.     91/01/20 AIH
  25.     - Uses main screen instead of GetGrayRgn for positioning a
  26.     rectangle in the screen
  27.     
  28.     91/01/05 Ari Halberstadt (AIH)
  29.     - Inserted this standard header in all files */
  30.  
  31. #include "ScreenLib.h"
  32. #include "RectangleLib.h"
  33.  
  34. /* true if rectangle is valid */
  35. Boolean RectValid(const Rect *r)
  36. {
  37.     return(r->top <= r->bottom && r->left <= r->right);
  38. }
  39.  
  40. /* convert rectangle in current port's coordinates to global coordinates */
  41. void RectLocalToGlobal(Rect *r)
  42. {
  43.     require(RectValid(r));
  44.     LocalToGlobal(&((Point *) r)[0]);
  45.     LocalToGlobal(&((Point *) r)[1]);
  46. }
  47.  
  48. /* convert rectangle in global coordinates to current port's coordinates */
  49. void RectGlobalToLocal(Rect *r)
  50. {
  51.     require(RectValid(r));
  52.     GlobalToLocal(&((Point *) r)[0]);
  53.     GlobalToLocal(&((Point *) r)[1]);
  54. }
  55.  
  56. /* convert rectangle in specified port's coordinates to global coordinates */
  57. void RectPortToGlobal(Rect *r, GrafPtr port)
  58. {
  59.     GrafPtr oldport;
  60.     
  61.     GetPort(&oldport);
  62.     SetPort(port);
  63.     RectLocalToGlobal(r);
  64.     SetPort(oldport);
  65. }
  66.  
  67. /* convert rectangle in global coordinates to specified port's coordinates */
  68. void RectGlobalToPort(Rect *r, GrafPtr port)
  69. {
  70.     GrafPtr oldport;
  71.     
  72.     GetPort(&oldport);
  73.     SetPort(port);
  74.     RectGlobalToLocal(r);
  75.     SetPort(oldport);
  76. }
  77.  
  78. /* return height of rectangle */
  79. short RectHeight(const Rect *r)
  80. {
  81.     require(RectValid(r));
  82.     return(r->bottom - r->top);
  83. }
  84.  
  85. /* return width of rectangle */
  86. short RectWidth(const Rect *r)
  87. {
  88.     require(RectValid(r));
  89.     return(r->right - r->left);
  90. }
  91.  
  92. /* return area of rectangle */
  93. long RectArea(const Rect *r)
  94. {
  95.     return(RectWidth(r) * RectHeight(r));
  96. }
  97.  
  98. /* true if r1 is completely contained within r2 */
  99. Boolean RectWithin(const Rect *r1, const Rect *r2)
  100. {
  101.     require(RectValid(r1));
  102.     require(RectValid(r2));
  103.     return(r1->left >= r2->left && r1->right <= r2->right &&
  104.                 r1->top >= r2->top && r1->bottom <= r2->bottom);
  105. }
  106.  
  107. /* true if r1 intersects r2 */
  108. Boolean RectIntersect(const Rect *r1, const Rect *r2)
  109. {
  110.     require(RectValid(r1));
  111.     require(RectValid(r2));
  112.     ensure(false); /* body of this function hasn't been written yet */
  113. }
  114.  
  115. /* Position rectangle r2 within rectangle r1. To position in the middle
  116.     horizontally and 1/3 of the way from the top, use h=2 and v=3. Point
  117.     is set to the top left coordinates to which r1 should be moved. */
  118. void RectPosition(const Rect *r1, const Rect *r2, short h, short v, Point *pt)
  119. {
  120.     require(RectValid(r1));
  121.     require(RectValid(r2));
  122.     require(h > 0);
  123.     require(v > 0);
  124.     pt->h = r1->left + (RectWidth(r1) - RectWidth(r2)) / h;
  125.     pt->v = r1->top + (RectHeight(r1) - RectHeight(r2)) / v;
  126. }
  127.  
  128. /* position the rectangle within the current screen */
  129. void RectPositionInScreen(const Rect *r, short h, short v, Point *pt)
  130. {
  131.     Rect scrn;
  132.  
  133.     ScreenGrayRect(&scrn);
  134.     RectPosition(&scrn, r, h, v, pt);
  135. }
  136.  
  137. /* move the rectangle to the specified position */
  138. void RectMove(Rect *r, short h, short v)
  139. {
  140.     short width, height;
  141.     
  142.     require(RectValid(r));
  143.     width = RectWidth(r);
  144.     height = RectHeight(r);
  145.     r->bottom = v + height;
  146.     r->right = h + width;
  147.     r->top = v;
  148.     r->left = h;
  149.     ensure(RectValid(r));
  150. }
  151.  
  152.  
  153.